package com.mdxx.base.realm;


import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Component;

import com.mdxx.patrol.system.bean.SysMember;
import com.mdxx.util.StringUtil;

import javax.annotation.Resource;

/**
 * Created by MD-01 on 2017/6/29.
 * 密码加密处理
 */
@Component
public class PasswordHelper {

//    @Resource
//    private GlobalParams globalParams;

    private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();

    /**
     * 加密
     * @param member
     */
    public void encryptPassword(SysMember member) {

        String salt = randomNumberGenerator.nextBytes().toHex();
        

        String newPassword = new SimpleHash("md5",
                member.getPassword(),
                ByteSource.Util.bytes(salt),
                2).toHex();

        member.setSalt(salt);
        member.setPassword(newPassword);
    }

    /**
     * 根据输入密码和盐值得到加密后的密码
     */
    public String calculatePassword(String inputPassword,String salt){

      
        String newPassword = new SimpleHash("md5",
                inputPassword,
                ByteSource.Util.bytes(salt),
               2)
                .toHex();
        return newPassword;
    }
}
